<template>
    <section>
        <!--高级查询form表单-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="searchForm">
                <el-form-item>
                    <el-input v-model="searchForm.name" placeholder="服务名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="searchForm.state" clearable  placeholder="请选择状态">
                        <el-option label="上架" value="1"></el-option>
                        <el-option label="下架" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="searchProducts">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                    <el-button type="primary" @click="onSale" :disabled="this.sels.length===0">上架</el-button>
                    <el-button type="primary" @click="offSale" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--服务列表-->
        <el-table :data="products" v-loading="listLoading"   @selection-change="selsChange" style="width: 100%;">
            <!--复选框-->
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60" label="序号">
            </el-table-column>
            <el-table-column prop="name" label="服务名">
            </el-table-column>
            <el-table-column prop="saleprice" label="售价">
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间">
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间">
            </el-table-column>
            <el-table-column prop="state" label="状态">
                <template scope="scope">
                    <span v-if="scope.row.state==1" style="color: green">上架</span>
                    <span v-if="scope.row.state==0" style="color: red">下架</span>
                </template>
            </el-table-column>
            <el-table-column prop="costprice" label="成本价">
            </el-table-column>
            <el-table-column prop="createtime" label="创建时间">
            </el-table-column>
            <el-table-column prop="salecount" label="销量">
            </el-table-column>
            <el-table-column label="操作" >
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>
        <!--新增界面-->
        <el-dialog title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" :rules="rules" ref="ruleForm"  label-width="80px" >
                <el-form-item label="商品名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="零售价" prop="saleprice">
                    <el-input v-model="addForm.saleprice" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="成本价" prop="costprice">
                    <el-input v-model="addForm.costprice" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="服务详情">
                    <!--<el-input v-model="addForm.intro"  auto-complete="off"></el-input>-->
                    <quill-editor v-model="addForm.intro"  :options="editorOption">
                    </quill-editor>
                </el-form-item>
                <el-form-item label="预约须知">
                    <quill-editor v-model="addForm.orderNotice"  :options="editorOption">
                    </quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" >提交</el-button>
            </div>
        </el-dialog>


    </section>
</template>
<script>
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    /*导入第三方的插件*/
    import quillConfig from "../../common/quill-config"

    export default {
        components: {
            quillEditor:quillEditor
        },
        data() {
            return {
                //富文本编辑器
                editorOption: quillConfig,
                //图片的，先不管
                fileList: [],
                rules: {
                    name: [
                        {required: true, message: '服务名必填', trigger: 'blur'}
                    ],
                },
                //添加修改form表单数据
                addForm:{
                    id:"",
                    //定义一个资源来装图片路径
                    resources:"",
                    //商品名
                    name:'',
                    //零售价
                    saleprice:'',
                    //状态  上架还是下架
                    state:'',
                    //成本价
                    costprice:"",
                    //详情
                    intro:"",
                    //预约须知
                    orderNotice:''

                },
                //默认不展示模态框
                addFormVisible:false,
                //高级查询form表单
                searchForm: {
                    //部门名
                    name: "",
                    state: ""
                },
                //服务分页列表
                products:[],
                //加载列表框
                listLoading:false,
                //当前页
                currentPage:1,
                //每页条数
                pageSize:10,
                //总条数
                total: 0,
                //封装复选框选中的行数据
                sels:[]
            };
        },
        methods: {
            //上架服务
            onSale(){
                //提示确认删除
                this.$confirm('您确认要上架吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/product/batchOnSale",this.sels).then(res=>{
                        let {success, msg} = res.data;
                        if(success){
                            this.$message({
                                message: '上架成功',
                                type: 'success'
                            });
                            //刷新界面
                            this.loadProducts();
                        }else{
                            this.$message.error('上架失败');
                        }
                    })
                }).catch(() => {

                });
            },
            offSale(){
                //提示确认删除
                this.$confirm('您确认要下架吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/product/batchOffSale",this.sels).then(res=>{
                        let {success, msg} = res.data;
                        if(success){
                            this.$message({
                                message: '上架成功',
                                type: 'success'
                            });
                            //刷新界面
                            this.loadProducts();
                        }else{
                            this.$message.error('上架失败');
                        }
                    })
                }).catch(() => {

                });
            },
            //添加修改提交按钮绑定事件
            addSubmit(){
                //添加服务详情
                let productDetail = {"intro": this.addForm.intro, "orderNotice": this.addForm.orderNotice};
                //添加到form表单
                this.addForm.productDetail = productDetail;
                //提交form表单
                this.$refs["ruleForm"].validate((valid) => {
                    //校验通过之后，执行if语句中的内容
                    if (valid) {
                        this.$http.post("/product", this.addForm).then(res => {
                            let {success, msg} = res.data;
                            if(success){
                                this.$message({
                                    type:success,
                                    message:"操做成功"
                                })
                                //关闭模态框
                                this.addFormVisible = false;
                                //刷新界面
                                this.loadProducts();
                            }else{
                                this.$message.error(msg);
                            }
                        });
                    }
                });
            },

            selsChange(rows){
                this.sels = rows;
            },
            //新增按钮触发事件
            handleAdd(){
                //设置添加标题
                this.title="添加";
                //先重置
                if(this.$refs["ruleForm"]){
                    //当出现了校验规则的时候，才能进行重置(上面判断的原因)
                    this.$refs["ruleForm"].resetFields();//this.$refs.ruleForm.resetFields()
                }
                //手动清空form表单
                for(var k in this.addForm){
                    this.addForm[k] = "";
                };
                //点击添加弹出模态框
                this.addFormVisible=true;
                //添加后默认下架状态
                this.addForm.state = 0;
            },
            //编辑按钮触发事件
            handleEdit(index,row){
                //修改标题
                this.title = "修改";
                //能解决大部分的属性
                for(var k in this.addForm){
                    this.addForm[k] = row[k];
                }
                //特殊字段你手动回显
                this.addForm.intro = row.productDetail.intro;
                //特殊字段你手动回显
                this.addForm.orderNotice = row.productDetail.orderNotice

                //弹出窗体
                this.addFormVisible = true;
            },
            //删除按钮触发事件
            handleDel(index,row){
                //提示确认删除
                this.$confirm('您确认离我而去吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("/product/"+row.id).then(res=>{
                        let {success, msg} = res.data;
                        if(success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            //刷新界面
                            this.loadProducts();
                        }else{
                            this.$message.error('错了哦，这是一条错误消息');
                        }
                    })

                }).catch(() => {

                });
            },
            //页码发生改变的时候触发此函数
            handleCurrentChange(val){
                //修改当前页
                this.currentPage = val;
                //再来分页查询
                this.loadProducts();
            },
            //批量删除
            batchRemove(){
                //提示确认删除
                this.$confirm('您确认离我而去吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/product/batchRemove",this.sels).then(res=>{
                        let {success, msg} = res.data;
                        if(success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            //刷新界面
                            this.loadProducts();
                        }else{
                            this.$message.error('错了哦，这是一条错误消息');
                        }
                    })
                }).catch(() => {

                });
            },
            //高级查询form表单
            searchProducts(){
                this.currentPage = 1;
                this.loadProducts();
            },
            //加载服务分页列表
            loadProducts(){
                //显示加载框
                this.listLoading = true;
                //前端传递的分页参数(当前页，每页条数)
                let param = {
                    "currentPage": this.currentPage, "pageSize": this.pageSize,
                    "name":this.searchForm.name,"state":this.searchForm.state
                };
                //请求url地址，并且传递参数
                this.$http.patch("/product",param).then(res=>{
                    //关闭加载框
                    this.listLoading = false;
                    let {total,list} = res.data;
                    //返回分页列表
                    this.products = list;
                    //返回总条数
                    this.total = total;
                })
            }

        },
        mounted() {
            this.loadProducts();
            //this.findProductDetail();
        }
    }

</script>